#include <iostream>
#include <string>

using namespace std;

class Solution {
public:
  int lengthOfLastWord(string s) {

    // int start =-1;
    // int end =0;
    int lastWordLen = 0;
    bool isWord = false;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == ' ') {
        isWord = false;
        continue;
      }
      if (isWord) {
        lastWordLen += 1;
      } else {
        lastWordLen = 1;
      }
      isWord = true;
    }

    return lastWordLen;
  }
};

void testcase02() {
  printf("\n%s Run\n", __FUNCTION__);
  string input = "   fly me   to   the moon  ";
  int want = 4;
  int got = 0;

  Solution so;
  got = so.lengthOfLastWord(input);
  if (got != want) {
    printf("failed! want:%d, but got:%d\n", want, got);
  } else {
    printf("success!\n");
  }
}
void testcase01() {
  printf("\n%s Run\n", __FUNCTION__);
  string input = "Hello World";
  int want = 5;
  int got = 0;

  Solution so;
  got = so.lengthOfLastWord(input);
  if (got != want) {
    printf("failed! want:%d, but got:%d\n", want, got);
  } else {
    printf("success!\n");
  }
}

int main() {
  testcase01();
  testcase02();
  return 0;
}
